package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

import java.util.List;

/**
 * Created by Czl on 2015/10/23.
 */
@SuppressWarnings("serial")
public class StudyPlan extends Model<StudyPlan> {
	
	public static final StudyPlan dao = new StudyPlan();
	
    public StudyPlan getStudyPlan(int year, String studyGroup){
        return dao.findFirst("select * from t_study_plan where year = ? and study_group = ?", year, studyGroup);
    }

    /**
	 * 获取学员学习计划中的学习课时数：按照课程类型必修选修和课程完成状态来查询
	 * @param studyPlanId 学习计划ID
	 * @param studentId 学员ID
	 * @param courseType 课程类型： 1 必修课  0 选修课
	 * @param learnedStatus 课程完成状态： 1 已完成  0 未完成
	 * @return
	 */
	public double getStudyCoursePeriodsInStudyplan(long studyPlanId, long studentId,
			String courseType, int learnedStatus) {
		
		String sql = "SELECT sum(setting.period) FROM t_course_student cs, t_course_base c, t_course_learning_setting setting"
				+ " where cs.course_id = c.id and c.id = setting.course_id"
				+ " and cs.target_id = ? and cs.target_type = ? and cs.student_id = ?"
				+ " and cs.course_type = ? and cs.learned_status = ?";
		Double result = Db.queryDouble(sql, studyPlanId, "studyplan", studentId, courseType, learnedStatus);
		return result!=null ? result : 0;
	}
    
    
}

